package com.cuanbo.dualscreenteachzhu.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuanbo.dualscreenteachzhu.R;
import com.cuanbo.dualscreenteachzhu.bean.EventBusBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class DrawBoradLayout extends FrameLayout implements View.OnClickListener, ColorSeekBar.OnTouchDataChangeListener {
    private ColorSeekBar colorSeekBar;
    private View v;
    private View vC1CK;
    private View vC2CK;
    private View vC3CK;
    private View vC4CK;
    private View vC5CK;
    private View vC6CK;
    private ImageView ivPen;
    private ImageView ivXpc;
    private View ivCircle;
    private View ivRect;
    private TextView tvStrokeWidth;
    private View ivRetract;
    private View ivShapeSelect;
    private View llSelectShape;
    private boolean selectShape=false;
    private View ivCloseSelectShape;
    private View ivNoRetract;
    private View ivQx;
    private ImageView ivTriangle;
    private ImageView ivLine;


    public DrawBoradLayout(Context context) {
        this(context, null);
    }

    public DrawBoradLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawBoradLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        v= LayoutInflater.from(getContext()).inflate(R.layout.draw_board_layout,null);
        addView(v);
        ivTriangle = v.findViewById(R.id.ivTriangle);
        ivLine = v.findViewById(R.id.ivLine);
        ivQx = v.findViewById(R.id.ivQx);
        ivNoRetract = v.findViewById(R.id.ivNoRetract);
        ivCloseSelectShape = v.findViewById(R.id.ivCloseSelectShape);
        llSelectShape = v.findViewById(R.id.llSelectShape);
        ivShapeSelect = v.findViewById(R.id.ivShapeSelect);
        ivRetract = v.findViewById(R.id.ivRetract);
        tvStrokeWidth = v.findViewById(R.id.tvStrokeWidth);
        ivCircle =v.findViewById(R.id.ivCircle);
        ivRect =v.findViewById(R.id.ivRect);
        ivPen =v.findViewById(R.id.ivPen);
        ivXpc=v.findViewById(R.id.ivXpc);
        colorSeekBar=v.findViewById(R.id.colorSeekBar);
        vC1CK=findViewById(R.id.vC1CK);
        vC2CK=findViewById(R.id.vC2CK);
        vC3CK=findViewById(R.id.vC3CK);
        vC4CK=findViewById(R.id.vC4CK);
        vC5CK=findViewById(R.id.vC5CK);
        vC6CK=findViewById(R.id.vC6CK);
        colorSeekBar.setNowProgress((int) DrawBoard.strokeWidth*4);
        colorSeekBar.setOnTouchDataChangeListener(this);
        findViewById(R.id.vC1).setOnClickListener(this);
        findViewById(R.id.vC2).setOnClickListener(this);
        findViewById(R.id.vC3).setOnClickListener(this);
        findViewById(R.id.vC4).setOnClickListener(this);
        findViewById(R.id.vC5).setOnClickListener(this);
        findViewById(R.id.vC6).setOnClickListener(this);
        findViewById(R.id.ivQx).setOnClickListener(this);
        findViewById(R.id.ivLine).setOnClickListener(this);
        findViewById(R.id.ivTriangle).setOnClickListener(this);
        findViewById(R.id.ivPen).setOnClickListener(this);
        findViewById(R.id.ivXpc).setOnClickListener(this);
        findViewById(R.id.ivRect).setOnClickListener(this);
        findViewById(R.id.ivClear).setOnClickListener(this);
        findViewById(R.id.ivCircle).setOnClickListener(this);
        findViewById(R.id.ivRetract).setOnClickListener(this);
        findViewById(R.id.ivNoRetract).setOnClickListener(this);
        findViewById(R.id.ivShapeSelect).setOnClickListener(this);
        findViewById(R.id.ivCloseSelectShape).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                selectShape=false;
                llSelectShape.setVisibility(View.GONE);
            }
        });
        findViewById(R.id.ivCloseDrawBoard).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new EventBusBean(EventBusBean.BUTTON_COMMENTS_OFF));
            }
        });
        EventBus.getDefault().register(this);
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.ivPen){
            pen();
            ivQx.setBackgroundResource(R.drawable.btn_check_bg);
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
        }else if(view.getId()==R.id.ivRetract){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.RETRACT));
        }else if(view.getId()==R.id.ivNoRetract){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.NORETRACT));
        }else if(view.getId()==R.id.ivShapeSelect){
            selectShape=!selectShape;
            if(selectShape){
                llSelectShape.setVisibility(View.VISIBLE);
            }else{
                llSelectShape.setVisibility(View.GONE);
            }
        }else if(view.getId()==R.id.ivRect){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            ivQx.setBackgroundColor(0x00000000);
            ivTriangle.setBackgroundColor(0x00000000);
            ivLine.setBackgroundColor(0x00000000);
            ivRect.setBackgroundColor(0x00000000);
            ivCircle.setBackgroundColor(0x00000000);
            tvStrokeWidth.setText("画笔粗细");
            ivRect.setBackgroundResource(R.drawable.btn_check_bg);
            ivXpc.setBackgroundColor(0x00000000);
            ivPen.setBackgroundResource(R.drawable.btn_check_bg);
            colorSeekBar.setNowProgress((int) DrawBoard.strokeWidth*4);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.RECT));
        }else if(view.getId()==R.id.ivCircle){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            ivQx.setBackgroundColor(0x00000000);
            ivTriangle.setBackgroundColor(0x00000000);
            ivLine.setBackgroundColor(0x00000000);
            ivRect.setBackgroundColor(0x00000000);
            ivCircle.setBackgroundColor(0x00000000);
            tvStrokeWidth.setText("画笔粗细");
            ivCircle.setBackgroundResource(R.drawable.btn_check_bg);
            ivXpc.setBackgroundColor(0x00000000);
            ivPen.setBackgroundResource(R.drawable.btn_check_bg);
            colorSeekBar.setNowProgress((int) DrawBoard.strokeWidth*4);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.CIRCLE));
        }else if(view.getId()==R.id.ivLine){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            ivQx.setBackgroundColor(0x00000000);
            ivTriangle.setBackgroundColor(0x00000000);
            ivLine.setBackgroundColor(0x00000000);
            ivRect.setBackgroundColor(0x00000000);
            ivCircle.setBackgroundColor(0x00000000);
            tvStrokeWidth.setText("画笔粗细");
            ivLine.setBackgroundResource(R.drawable.btn_check_bg);
            ivXpc.setBackgroundColor(0x00000000);
            ivPen.setBackgroundResource(R.drawable.btn_check_bg);
            colorSeekBar.setNowProgress((int) DrawBoard.strokeWidth*4);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.LINE));
        }else if(view.getId()==R.id.ivTriangle){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            ivQx.setBackgroundColor(0x00000000);
            ivTriangle.setBackgroundColor(0x00000000);
            ivLine.setBackgroundColor(0x00000000);
            ivRect.setBackgroundColor(0x00000000);
            ivCircle.setBackgroundColor(0x00000000);
            tvStrokeWidth.setText("画笔粗细");
            ivTriangle.setBackgroundResource(R.drawable.btn_check_bg);
            ivXpc.setBackgroundColor(0x00000000);
            ivPen.setBackgroundResource(R.drawable.btn_check_bg);
            colorSeekBar.setNowProgress((int) DrawBoard.strokeWidth*4);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.TRIANGLE));
        }else if(view.getId()==R.id.ivQx){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            ivQx.setBackgroundColor(0x00000000);
            ivTriangle.setBackgroundColor(0x00000000);
            ivLine.setBackgroundColor(0x00000000);
            ivRect.setBackgroundColor(0x00000000);
            ivCircle.setBackgroundColor(0x00000000);
            tvStrokeWidth.setText("画笔粗细");
            ivQx.setBackgroundResource(R.drawable.btn_check_bg);
            ivXpc.setBackgroundColor(0x00000000);
            ivPen.setBackgroundResource(R.drawable.btn_check_bg);
            colorSeekBar.setNowProgress((int) DrawBoard.strokeWidth*4);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.PEN));
        }else if(view.getId()==R.id.ivXpc){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            ivPen.setBackgroundColor(0x00000000);
            ivXpc.setBackgroundColor(0x00000000);
            ivRect.setBackgroundColor(0x00000000);
            ivCircle.setBackgroundColor(0x00000000);
            tvStrokeWidth.setText("橡皮擦粗细");
            ivXpc.setBackgroundResource(R.drawable.btn_check_bg);
            colorSeekBar.setNowProgress((int) DrawBoard.clearStrokeWidth/2);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.ERASER));
        }else if(view.getId()==R.id.ivClear){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.DRAWBOARD_CLEAR));
        }else if(view.getId()==R.id.vC1){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            vC1CK.setVisibility(View.VISIBLE);
            vC2CK.setVisibility(View.GONE);
            vC3CK.setVisibility(View.GONE);
            vC4CK.setVisibility(View.GONE);
            vC5CK.setVisibility(View.GONE);
            vC6CK.setVisibility(View.GONE);
            EventBusBean eventBusBean=new EventBusBean(EventBusBean.UPDATE_DRAWBOARD_PARAMETER);
            eventBusBean.setData(view.getId());
            EventBus.getDefault().post(eventBusBean);
        }else if(view.getId()==R.id.vC2){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            vC1CK.setVisibility(View.GONE);
            vC2CK.setVisibility(View.VISIBLE);
            vC3CK.setVisibility(View.GONE);
            vC4CK.setVisibility(View.GONE);
            vC5CK.setVisibility(View.GONE);
            vC6CK.setVisibility(View.GONE);
            EventBusBean eventBusBean=new EventBusBean(EventBusBean.UPDATE_DRAWBOARD_PARAMETER);
            eventBusBean.setData(view.getId());
            EventBus.getDefault().post(eventBusBean);
        }else if(view.getId()==R.id.vC3){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            vC1CK.setVisibility(View.GONE);
            vC2CK.setVisibility(View.GONE);
            vC3CK.setVisibility(View.VISIBLE);
            vC4CK.setVisibility(View.GONE);
            vC5CK.setVisibility(View.GONE);
            vC6CK.setVisibility(View.GONE);
            EventBusBean eventBusBean=new EventBusBean(EventBusBean.UPDATE_DRAWBOARD_PARAMETER);
            eventBusBean.setData(view.getId());
            EventBus.getDefault().post(eventBusBean);
        }else if(view.getId()==R.id.vC4){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            vC1CK.setVisibility(View.GONE);
            vC2CK.setVisibility(View.GONE);
            vC3CK.setVisibility(View.GONE);
            vC4CK.setVisibility(View.VISIBLE);
            vC5CK.setVisibility(View.GONE);
            vC6CK.setVisibility(View.GONE);
            EventBusBean eventBusBean=new EventBusBean(EventBusBean.UPDATE_DRAWBOARD_PARAMETER);
            eventBusBean.setData(view.getId());
            EventBus.getDefault().post(eventBusBean);
        }else if(view.getId()==R.id.vC5){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            vC1CK.setVisibility(View.GONE);
            vC2CK.setVisibility(View.GONE);
            vC3CK.setVisibility(View.GONE);
            vC4CK.setVisibility(View.GONE);
            vC5CK.setVisibility(View.VISIBLE);
            vC6CK.setVisibility(View.GONE);
            EventBusBean eventBusBean=new EventBusBean(EventBusBean.UPDATE_DRAWBOARD_PARAMETER);
            eventBusBean.setData(view.getId());
            EventBus.getDefault().post(eventBusBean);
        }else if(view.getId()==R.id.vC6){
            selectShape=false;
            llSelectShape.setVisibility(View.GONE);
            vC1CK.setVisibility(View.GONE);
            vC2CK.setVisibility(View.GONE);
            vC3CK.setVisibility(View.GONE);
            vC4CK.setVisibility(View.GONE);
            vC5CK.setVisibility(View.GONE);
            vC6CK.setVisibility(View.VISIBLE);
            EventBusBean eventBusBean=new EventBusBean(EventBusBean.UPDATE_DRAWBOARD_PARAMETER);
            eventBusBean.setData(view.getId());
            EventBus.getDefault().post(eventBusBean);
        }

    }

    private void pen(){
        ivPen.setBackgroundColor(0x00000000);
        ivXpc.setBackgroundColor(0x00000000);
        tvStrokeWidth.setText("画笔粗细");
        ivPen.setBackgroundResource(R.drawable.btn_check_bg);
        colorSeekBar.setNowProgress((int) DrawBoard.strokeWidth*4);
        EventBus.getDefault().post(new EventBusBean(EventBusBean.PEN));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(EventBusBean eventBusBean) {
        if(eventBusBean.getType()== EventBusBean.BUTTON_COMMENTS_NO){
            setVisibility(View.VISIBLE);
        }else if(eventBusBean.getType()== EventBusBean.BUTTON_COMMENTS_OFF){
            setVisibility(View.GONE);
            pen();
        }
    }

    @Override
    public void onChange(int nowProgress, boolean isActionUp) {
        if(isActionUp){
            EventBusBean eventBusBean=new EventBusBean(EventBusBean.DRAWBOARD_SEEKBAR);
            eventBusBean.setData(nowProgress);
            EventBus.getDefault().post(eventBusBean);
        }
    }
}
